var startTime="00:00:00";
var endTime="23:59:59";

var table;
//相关业务逻辑
$(function(){
	page.bind();
	page.getWorkShopName();
//	$(".startTime").val(startTime);
//	$(".endTime").val(endTime);
});
var time;

function dataChange(){
	
}


var page={//页面总变量
		bind:function(){
			$("[name='weekCheck']").bind("click",function(){
				var week=$(this).val();
				if(week==8){
					$("[name='weekCheck']").prop("checked",false);
					$(this).prop("checked",true);
				}else{
					$(".toDay").prop("checked",false);
				}
			});
			
			
			
			$("#getPowerBtn").bind("click",function(){
				window.clearInterval(time);
				$("#powerTable").show();
				var workShopId=$("#workShopName option:selected").val();
				page.getPowerDataOnTime(workShopId);
				time=setInterval("table.ajax.reload( null, false)", 3000);//每秒刷新一次表格
			});
			
			//设置时间
			$("#setTimeBtn").bind("click",function(){
				page.setTime();
			});
			
			$('#powerTable tbody').on('click', '.zero', function () {
				var id=$(this).val();
				page.setPowerAction(id,0);
			});
			$('#powerTable tbody').on('click', '.one', function () {
				var id=$(this).val();
				page.setPowerAction(id,1);
			});
			$('#powerTable tbody').on('click', '.two', function () {
				var id=$(this).val();
				page.setPowerAction(id,2);
			});
			$('#powerTable tbody').on('click', '.three', function () {
				var id=$(this).val();
				page.setPowerAction(id,3);
			});
			$('#powerTable tbody').on('click', '.setWorkShop', function () {
				var id=$(this).val();
				$("#upId").val(id);
				$("#setWorkShopModal").modal("show");
			});
			
			$('#powerTable tbody').on('click', '.setTime', function () {
				var powerId=$(this).val();
				$("#timeId").val(powerId);
				var weekArray=null;
				ajaxUtil.ajaxRuquest('device/getPowerTimeByPowerId',{"powerId":powerId},function(data){
				     console.log(data);
				     if(data.length>0){
				    	 var week=data[0].week;
				    	 weekArray=week.split(",");
				    	 for(var i=0;i<weekArray.length;i++){
				    		 if(weekArray[i]=="星期一"){
				    			 $(".check1").prop("checked",true);
				    			 $(".toDay").prop("checked",false);
				    			 continue;
				    		 }
				    		 if(weekArray[i]=="星期二"){
				    			 $(".check2").prop("checked",true);
				    			 $(".toDay").prop("checked",false);
				    			 continue;
				    		 }
				    		 if(weekArray[i]=="星期三"){
				    			 $(".check3").prop("checked",true);
				    			 $(".toDay").prop("checked",false);
				    			 continue;
				    		 }
				    		 if(weekArray[i]=="星期四"){
				    			 $(".check4").prop("checked",true);
				    			 $(".toDay").prop("checked",false);
				    			 continue;
				    		 }
				    		 if(weekArray[i]=="星期五"){
				    			 $(".check5").prop("checked",true);
				    			 $(".toDay").prop("checked",false);
				    			 continue;
				    		 }
				    		 if(weekArray[i]=="星期六"){
				    			 $(".check6").prop("checked",true);
				    			 $(".toDay").prop("checked",false);
				    			 continue;
				    		 }
				    		 if(weekArray[i]=="星期日"){
				    			 $(".check7").prop("checked",true);
				    			 $(".toDay").prop("checked",false);
				    			 continue;
				    		 }
				    		 if(weekArray[i]=="今天"){
				    			 $(".check").prop("checked",false);
				    			 $(".toDay").prop("checked",true);
				    			 continue;
				    		 }
				    	 }
				     }
				     
				     for(var i=0;i<data.length;i++){
				    	 if(data[i].type==0){
				    		 $(".zeroStartTime").val(data[i].startTime);
				    		 $(".zeroEndTime").val(data[i].endTime);
				    		 continue;
				    	 }
				    	 if(data[i].type==1){
				    		 $(".oneStartTime").val(data[i].startTime);
				    		 $(".oneEndTime").val(data[i].endTime);
				    		 continue;
				    	 }
				    	 if(data[i].type==2){
				    		 $(".twoStartTime").val(data[i].startTime);
				    		 $(".twoEndTime").val(data[i].endTime);
				    		 continue;
				    	 }
				    	 if(data[i].type==3){
				    		 $(".threeStartTime").val(data[i].startTime);
				    		 $(".threeEndTime").val(data[i].endTime);
				    		 continue;
				    	 }
				     }
				});
				$("#setTimeModal").modal("show");
			});
			
			$("#setWorkShopBtn").bind("click",function(){
				var id=$("#upId").val();
				var workShopId=$("#setWorkShop option:selected").val();
				ajaxUtil.ajaxRuquest('device/setPowerWorkShop',{"id":id,"workShopId":workShopId},function(data){
					layer.msg(data,{icon:1});
					$("#setWorkShopModal").modal("hide")
					table.ajax.reload( null, false);
					window.clearInterval(time);
					time=setInterval("table.ajax.reload( null, false)", 3000);//每秒刷新一次表格
				});
			});
		},
		getPowerDataOnTime:function(workShopId){
			table=ajaxUtil.dataTablesAjax("powerTable",10,"device/getPowerDataOnTime",
					{'workShopId':workShopId},[
					{"data":"status",render: function (data,type,row) {
						var str="";
						switch (data) {
			                    case "0":
			                    	str="<button class='btn btn-default radius'>电气均关</button>";
			                        break;
			                    case "1":
			                    	str="<button class='btn btn-primary radius'>电已开</button>";
			                        break;
			                    case "2":
			                    	str="<button class='btn btn-secondary radius'>气已开</button>";
			                        break;
			                    case "3":
			                    	str="<button class='btn btn-success radius'>电气均开</button>";
			                    	break;
	                           }
			            return str;
			          }},
					{"data":"devicePower"},
					{"data":"electricity"},
					{"data":"workShopName"},
					{"data":"id",render: function (data,type,row){
						    return "<button class='zero btn btn-default radius' value='"+data+"'>关气关电</button>&nbsp;" +
							"<button class='one btn btn-primary radius' value='"+data+"'>只开电</button>&nbsp;"+
							"<button class='two btn btn-secondary radius' value='"+data+"'>只开气</button>&nbsp;"+
							"<button class='three btn btn-success radius' value='"+data+"'>开气开电</button>&nbsp;"+
							"<button class='setTime btn btn-success radius' value='"+data+"'>设置时间</button>&nbsp;"+
					        "<button class='setWorkShop btn btn-warning radius' value='"+data+"'>设置车间</button>"}
					}
			]);
		},
		getWorkShopName:function(){
			ajaxUtil.ajaxRuquest('basicData/getWorkshop',null,function(data){
				var html="<option value=''>全部</option>";
				for(var i=0;i<data.length;i++){
					html=html+"<option value='"+data[i].id+"'>"+data[i].workshopName+"</option>";
				}
				$("#workShopName").append(html);
				$("#setWorkShop").append(html);
			});
		},
		setPowerAction:function(id,action){
			ajaxUtil.ajaxRuquest('device/setPowerAction',{"id":id,"action":action},function(data){
				layer.msg(data,{icon:1});
				table.ajax.reload( null, false);
				window.clearInterval(time);
				time=setInterval("table.ajax.reload( null, false)", 3000);//每秒刷新一次表格
			});
		},
		setTime:function(){
			var powerId=$("#timeId").val();
			var week="";
			$("[name='weekCheck']:checked").each(function(){  
				week+=","+$(this).val();;
	    	});
			if(week==""){
				layAlert("请选择生效日期");
				return false;
			}
			var timeArray=new Array();
			var zeroStartTime=$(".zeroStartTime").val();
			var zeroEndTime=$(".zeroEndTime").val();
			
			var oneStartTime=$(".oneStartTime").val();
			var oneEndTime=$(".oneEndTime").val();
			
			var twoStartTime=$(".twoStartTime").val();
			var twoEndTime=$(".twoEndTime").val();
			
//			var threeStartTime=$(".threeStartTime").val();
//			var threeEndTime=$(".threeEndTime").val();
			
			if(zeroStartTime!="" && zeroEndTime!=""){
				var timeObject=new Object();
				timeObject.type=0;
				timeObject.startTime="2017-01-01 "+zeroStartTime;
				timeObject.endTime="2017-01-01 "+zeroEndTime;
				timeObject.week=week;
				timeObject.powerId=powerId;
				timeArray.push(timeObject);
			}
			
			if(oneStartTime!="" && oneEndTime!=""){
				var timeObject1=new Object();
				timeObject1.type=1;
				timeObject1.startTime="2017-01-01 "+oneStartTime;
				timeObject1.endTime="2017-01-01 "+oneEndTime;
				timeObject1.week=week;
				timeObject1.powerId=powerId;
				timeArray.push(timeObject1);
			}
			
			if(twoStartTime!="" && twoEndTime!=""){
				var timeObject2=new Object();
				timeObject2.type=2;
				timeObject2.startTime="2017-01-01 "+twoStartTime;
				timeObject2.endTime="2017-01-01 "+twoEndTime;
				timeObject2.week=week;
				timeObject2.powerId=powerId;
				timeArray.push(timeObject2);
			}
			
//			if(threeStartTime!="" && threeEndTime!=""){
//				var timeObject3=new Object();
//				timeObject3.type=3;
//				timeObject3.startTime="2017-01-01 "+threeStartTime;
//				timeObject3.endTime="2017-01-01 "+threeEndTime;
//				timeObject3.week=week;
//				timeObject3.powerId=powerId;
//				timeArray.push(timeObject3);
//			}
		  var returnArray=new Array();
		  if(timeArray.length>0){
			  if(timeArray.length==1){
				  if(timeArray[0].startTime!="" && timeArray[0].endTime!=""){
					  var x=new Date(timeArray[0].startTime);
					  var y=new Date(timeArray[0].endTime);
					  if(x>=y){
						  layAlert("开始时间大于结束时间");
						  return false;
					  }
					  returnArray.push(timeArray[0]);
				  }
			  }
			  //本身遍历
			  for(var i=0;i<timeArray.length;i++){
                  //比较遍历
				  for(var j=0;j<timeArray.length;j++){
					  //如果本身两个时间不为空
					  if(timeArray[i].startTime!="" && timeArray[i].endTime!=""){
						  //比较两个时间不为空
						   if(timeArray[j].startTime && timeArray[j].endTime){
							 //不与本身比较
							   if(i==j){
								   continue;
							   }
							   var one=new Date(timeArray[i].startTime);
							   var two=new Date(timeArray[i].endTime);
							   var three=new Date(timeArray[j].startTime);
							   var four=new Date(timeArray[j].endTime);
							   if(one>=two || three>=four){
								   layAlert("开始时间大于结束时间");
								   return false;
							   }
							   if(one>=three && one<=four){
								   layAlert("时间区间有重叠，请检查时间");
								   return false;
							   }
							   if(two>=three && two<=four){
								   layAlert("时间区间有重叠，请检查时间");
								   return false;
							   }
						   }
					  }
				  }
				  returnArray.push(timeArray[i]);
			  }
		  }else{
			  layAlert("请选择时间");
			  return false;
		  }
		  if(returnArray.length<=0){
			  layAlert("请设置完整开始与结束时间");
			  return false;
		  }
		  ajaxUtil.ajaxRuquest('device/setTime',{"timeArray":JSON.stringify(returnArray),"powerId":powerId},function(data){
			  layer.msg(data,{icon:1});
			  $("#setTimeModal").modal("hide");
		  });
		  
		}
}


